package com.xl6667.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl6667.constants.SystemConstants;
import com.xl6667.domain.ResponseResult;
import com.xl6667.domain.dto.AddUser;
import com.xl6667.domain.dto.RoleDto;
import com.xl6667.domain.dto.SysUserDto;
import com.xl6667.domain.dto.UpdateUser;
import com.xl6667.domain.entity.Role;
import com.xl6667.domain.entity.User;
import com.xl6667.domain.entity.UserRole;
import com.xl6667.mapper.UserRoleMapper;
import com.xl6667.service.RoleService;
import com.xl6667.service.UserRoleService;
import com.xl6667.service.UserService;
import com.xl6667.util.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 熊磊
* @description 针对表【sys_user_role(用户和角色关联表)】的数据库操作Service实现
* @createDate 2024-03-21 13:22:23
*/
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole>
    implements UserRoleService{
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Override
    @Transactional
    public ResponseResult addUser(AddUser addUser) {

        User user = BeanCopyUtil.copyBean(addUser, User.class);
        userService.register(user);

        List<UserRole> userRoles = addUser.getRoleIds().stream().map(roleId -> new UserRole(user.getId(), roleId)).collect(Collectors.toList());
        saveBatch(userRoles);

        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult getUserAndRole(Long id) {
        //查询用户信息
        User user = userService.getById(id);
        SysUserDto sysUserDto = BeanCopyUtil.copyBean(user, SysUserDto.class);
        //查询用户权限id
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,id);
        List<Long> roleIds = list(queryWrapper).stream().map(UserRole::getRoleId).collect(Collectors.toList());
        //查询所有权限信息
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.eq(Role::getStatus, SystemConstants.STATUS_NORMA);
        roleLambdaQueryWrapper.eq(Role::getDelFlag,SystemConstants.DEL_FLAG_NORMA);

        List<Role> roles = roleService.list(roleLambdaQueryWrapper);
        List<RoleDto> roleDtos = BeanCopyUtil.copyBeanList(roles, RoleDto.class);
        UpdateUser updateUser = new UpdateUser(sysUserDto,roleIds,roleDtos);
        return ResponseResult.okResult(updateUser);
    }

    @Override
    @Transactional
    public ResponseResult putUser(AddUser addUser) {
        User user = BeanCopyUtil.copyBean(addUser, User.class);
        userService.updateUser(user);

        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,user.getId());
        remove(queryWrapper);

        List<UserRole> userRoles = addUser.getRoleIds().stream().map(roleId -> new UserRole(user.getId(), roleId)).collect(Collectors.toList());
        saveBatch(userRoles);

        return ResponseResult.okResult("修改成功");
    }
}




